<?php

class PackageOrderPage extends MemberPage {
    private static $default_parent = 'PurchaseOrderPage';
    
    private static $db = array();

    private static $has_one = array();
}

class PackageOrderPage_Controller extends MemberPage_Controller {
    private static $allowed_actions = array(
        'Form',
        'view_order',
        'download_order',
        'print_order'
    );

    function init() {
        parent::init();
    }

    function Form() {
        $fields = FieldList::create(
            TextField::create('ID', _t('PackageOrderPage.ORDER_ID', 'Order ID')),
            DateRangeField::create('Created', _t('PackageOrderPage.ORDER_DATE', 'Order Date')),
            singleton('OrderPackage')->obj('Status')->formField(_t('PackageOrderPage.STATUS', 'Status'), 'Status')->setEmptyString(_t('PackageOrderPage.ALL', 'All'))
        );

        $field_list = array(
            'Created' => _t('PackageOrderPage.ORDER_DATE', 'Order Date'),
            'ID' => _t('PackageOrderPage.ORDER_ID', 'Order ID'),
            'BV' => array('title' => _t('PackageOrderPage.BV', 'BV'), 'classes' => 'text-right'),
            'Price' => array('title' => _t('PackageOrderPage.PRICE', 'Price ({price})', '', array('price' => SiteCurrencyConfig::current_site_currency())), 'classes' => 'text-right'),
            'Status' => _t('PackageOrderPage.STATUS', 'Status'),
            'Action' => ''
        );
        $casting_list = array(
            'Created' => 'Datetime->Nice',
            'BV' => 'Int->Nice',
            'Price' => 'Double->Nice',
            'Status' => array('Dropdown->Title', 'OrderPackageStatusList')
        );

        return DataListSearchForm::create($this, 'Form', 'OrderPackage', array('SoldByID' => $this->CurrentMember()->ID), $fields)->setDataFieldList($field_list)->setFieldCasting($casting_list);
    }

    function view_order() {
        $id = (int)$this->request->param('ID');
		if($order_package = OrderPackage::get()->filter('SoldByID', $this->CurrentMember()->ID)->byID($id)){
	        $order_package->PrintLink = $this->Link('print_order/' . $id);
			if($order_package->config()->allowed_pdf && class_exists('SS_DOMPDF')){
	        	$order_package->PDFLink = $this->Link('download_order/' . $id);
	        }
	
	        return $order_package->FullDetailHTML;
		}
		
		return $this->httpError('404');
    }

    function download_order() {
        $id = (int)$this->request->param('ID');
		$order_package = OrderPackage::get()->filter('SoldByID', $this->CurrentMember()->ID)->byID($id);
		if($order_package && $order_package->config()->allowed_pdf && class_exists('SS_DOMPDF')){
        	return $order_package->ViewPDF();
		}
		
		return $this->httpError('404');
    }

    function print_order() {
        $id = (int)$this->request->param('ID');
		if($order_package = OrderPackage::get()->filter('SoldByID', $this->CurrentMember()->ID)->byID($id)){
        	return $order_package->ViewPrint();
		}
		
		return $this->httpError('404');
    }
}
?>